package com.example.demo.sys.web;

import com.example.demo.base.BaseController;
import com.example.demo.common.shiro.ShiroUtils;
import com.example.demo.sys.menu.entity.SysMenu;
import com.example.demo.sys.menu.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

import static com.example.demo.common.utils.MenuUtil.rootTree;

/**
 * 权限系统的页面跳转控制器
 *
 * @author haocj
 */
@Controller
@RequestMapping
public class WebController extends BaseController {

    @Autowired
    private SysMenuService menuService;

    /**
     * 登陆页面
     *
     * @return
     */
    @RequestMapping("/login")
    public String index() {
        return "login";
    }

    /**
     * 首页，加载动态菜单
     *
     * @return
     */
    @RequestMapping("/sys/index")
    public String mainIndex(SysMenu sysMenu, Model model) {
        sysMenu.setIsShows("0");
        List<SysMenu> listAll = menuService.findList(sysMenu);
        List<SysMenu> list = new ArrayList<>();
        for (SysMenu menu : listAll) {
            if (menu.getPermission() != null && !"".equals(menu.getPermission())) {
                boolean b = ShiroUtils.getSubjct().isPermitted(menu.getPermission());
                if (b) {
                    list.add(menu);
                }
            }
        }
        model.addAttribute("menus", rootTree(list));
        return "sys/index";
    }

    /**
     * 权限丢失页面
     *
     * @return
     */
    @RequestMapping("/sys/hasPermission")
    public String permission() {
        return "sys/permission";
    }

    /**
     * 菜单管理
     *
     * @return
     */
    @RequestMapping(value = "/sysMenu/list")
    public String getMenuInfo() {
        return "sys/menu/sys_menu";
    }

    /**
     * 角色管理
     *
     * @return
     */
    @RequestMapping(value = "/sysRole/list")
    public String findList() {
        return "sys/role/sys_role";
    }

    /**
     * 用户列表
     *
     * @return
     */
    @RequestMapping(value = "/sysUser/list")
    public String list() {
        return "sys/user/sys_user";
    }

    /**
     * 部门列表
     *
     * @return
     */
    @RequestMapping(value = "/sysDept/list")
    public String deptList() {
        return "sys/dept/sys_dept";
    }

    /**
     * 岗位列表
     *
     * @return
     */
    @RequestMapping(value = "/sysPost/list")
    public String psotList() {
        return "sys/post/sys_post";
    }


}
